<?php

namespace App\Providers;

use App\Models\RepayRecord;
use App\Models\UserLoanRequest;
use App\Observers\PayCompensateObserver;
use App\Observers\UserLoanRequestObserver;
use Illuminate\Support\ServiceProvider;

class AppServiceProvider extends ServiceProvider
{
    /**
     * Bootstrap any application services.
     *
     * @return void
     */
    public function boot()
    {
        UserLoanRequest::observe(UserLoanRequestObserver::class);
        if(\App::environment('local') && !\App::runningInConsole()){
            \DB::listen(function ($query) {
                $tmp       = str_replace('?', '"' . '%s' . '"', $query->sql);
                $qBindings = [];
                foreach ($query->bindings as $key => $value) {
                    if (is_numeric($key)) {
                        $qBindings[] = $value;
                    }else {
                        $tmp = str_replace(':' . $key, '"' . $value . '"', $tmp);
                    }
                }
                if(!strstr($tmp,'date_format') && !strstr($tmp,'DATE_FORMAT')){
                    $tmp = vsprintf($tmp, $qBindings);
                    $tmp = str_replace("\\", "", $tmp);
                    \Log::debug(' execution time: ' . $query->time . 'ms; ' . $tmp . "\n\n\t");
                }
            });
        }
    }

    /**
     * Register any application services.
     *
     * @return void
     */
    public function register()
    {
        //
    }
}
